/*
 * 文件名：CouponInfoServiceImpl.java
 * 版权：Copyright by www.huawei.com
 * 描述：
 * 修改人：lilan
 * 修改时间：2016年4月21日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.zcsy.manage.service.mall.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zcsy.commons.unique.UniqueNumberUtils;
import com.zcsy.commons.util.DateUtils;
import com.zcsy.manage.model.mall.CouponDetail;
import com.zcsy.manage.model.mall.CouponInfo;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.persistence.mall.CouponDetailDao;
import com.zcsy.manage.persistence.mall.CouponInfoDao;
import com.zcsy.manage.service.mall.ICouponDetailService;
import com.zcsy.manage.service.mall.ICouponInfoService;

/**
 * 
 * @Title: CouponInfoServiceImpl.java
 * @Package： com.zcsy.manage.service.mall.impl 
 * @Description: 优惠券信息service层接口实现类
 *
 * @author lilan 1063972142@qq.com
 * @Copyright: 2015 武汉恒数利通信息技术有限公司 All rights reserved.
 * @date 2016年4月21日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Service
public class CouponInfoServiceImpl implements ICouponInfoService{
	
	@Autowired
	private CouponInfoDao couponInfoDao;
	
	@Autowired
	private ICouponDetailService couponDetailService;

	@Override
	public int insert(CouponInfo couponInfo) {
		return couponInfoDao.insert(couponInfo);
	}

	@Override
	public int update(CouponInfo couponInfo) {
		CouponInfo oldInfo = couponInfoDao.selectByPrimaryKey(couponInfo.getId());
		
		oldInfo.setName(couponInfo.getName());
		oldInfo.setType(couponInfo.getType());
		oldInfo.setBreakLine(couponInfo.getBreakLine());
		oldInfo.setBreakPrice(couponInfo.getBreakPrice());
		oldInfo.setBeginDate(couponInfo.getBeginDate());
		oldInfo.setEndDate(couponInfo.getEndDate());
		oldInfo.setCount(couponInfo.getCount());
		oldInfo.setUseBeginDate(couponInfo.getUseBeginDate());
		oldInfo.setUseEndDate(couponInfo.getUseEndDate());
		oldInfo.setState(couponInfo.getState());
		oldInfo.setUserAccountType(couponInfo.getUserAccountType());
		oldInfo.setUserAccount(couponInfo.getUserAccount());
		oldInfo.setCellId(couponInfo.getCellId());
		
		return couponInfoDao.update(oldInfo);
	}

	@Override
	public int deleteByPrimaryKey(String id) {
		return couponInfoDao.deleteByPrimaryKey(id);
	}

	@Override
	public CouponInfo selectByPrimaryKey(String id) {
		return couponInfoDao.selectByPrimaryKey(id);
	}

	@Override
	public List<CouponInfo> selectByApplyKeyword(CouponInfo couponInfo) {
		return couponInfoDao.selectByApplyKeyword(couponInfo);
	}

	@Override
	public List<CouponInfo> selectByKeyword(CouponInfo couponInfo) {
		return couponInfoDao.selectByKeyword(couponInfo);
	}
	
	@Override
    public List<CouponInfo> selectByUserAccountAndState(CouponInfo couponInfo) {
        return couponInfoDao.selectByUserAccountAndState(couponInfo);
    }

	@Override
	public List<CouponInfo> getAllCouponInfo() {
		return couponInfoDao.getAllCouponInfo();
	}

	@Override
	public List<CouponInfo> selectByTime(CouponInfo couponInfo) {
		return couponInfoDao.selectByTime(couponInfo);
	}

	@Override
	public List<CouponInfo> selectCouponinfo(CouponInfo couponInfo) {
		return couponInfoDao.selectCouponinfo(couponInfo);
	}
	
    @Override
	public List<CouponInfo> getCouponInfoByShopId(String shopId) {
        return couponInfoDao.getCouponInfoByShopId(shopId);
        
    }
    
    @Override
    public List<CouponInfo> getAllCouponInfoByShopId(Map<String, Object> map) {
    	return couponInfoDao.getAllCouponInfoByShopId(map);
    } 

	@Override
	public List<CouponInfo> getAllCouponInfoBySId(String shopId) {
		return couponInfoDao.getAllCouponInfoBySId(shopId);
	} 
	
    @Override
    public List<CouponInfo> getFlatCouponInfo(String cellId) {
    	return couponInfoDao.getFlatCouponInfo(cellId);
    } 
    
    @Override
    public List<CouponInfo> getPlatformCouponInfo() {
        return couponInfoDao.getPlatformCouponInfo();
    }

	@Override
	public int updateCouponCount(CouponInfo couponInfo) {
		couponInfo.setCount(couponInfo.getCount() - 1);
		return couponInfoDao.update(couponInfo);
	}
	
	@Override
    public List<CouponInfo> getCouponInfo(String shopId) {
        return couponInfoDao.getCouponInfo(shopId);
    }
	
	@Override
    public List<CouponInfo> getflatCouponInfo() {
        return couponInfoDao.getflatCouponInfo();
    }
	
	@Override
	public String saveCoupons(String ids, User user){
		String result = null;
		if(ids != null){
			String couponIds[] = ids.split("\\,");
			for (int i = 0; i < couponIds.length; i++) {
				
				CouponInfo couponInfo = couponInfoDao.selectByPrimaryKey(couponIds[i]);
				
				if(couponInfo.getCount() > 0) {
					
					CouponDetail cDetail = couponDetailService.selectByUserAccountAndCouponId(couponIds[i], user.getLoginname());
					
					if(cDetail == null) {
						
						CouponDetail couponDetail = new CouponDetail();
						//添加领取优惠券信息
				        String id = UniqueNumberUtils.get18UniqueNumber();
				        couponDetail.setId(id);
				        couponDetail.setCouponId(couponIds[i]);
				        couponDetail.setState(couponInfo.getState());
				        couponDetail.setPhone(user.getPhone());
				        couponDetail.setUserAccount(user.getLoginname());
				        couponDetail.setExpireTime(DateUtils.str2Date(couponInfo.getUseEndDate() , DateUtils.date_sdf));
				        couponDetail.setCreateTime(DateUtils.gettimestamp());
				        couponDetailService.insert(couponDetail);
				        
				        couponInfo.setCount(couponInfo.getCount() - 1);
				        couponInfoDao.update(couponInfo);
				        
					} else {
						result = "请选择未领取过的优惠劵，您已领取优惠劵" + couponInfo.getName();
						break;
					}
				} else {
					result = couponInfo.getName() + "优惠劵已被领完,请重新选择！" ;
					break;
				}
				
			}
			if(result == null) {
				result = "领取优惠劵成功！";
			}
			
		} else {
			result = "领取失败！";
		}
		return result;
	}

}
